<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class UserLikeCategory extends Model
{
    protected $fillable = ['user_id','category_id','score'];
    public $timestamps = false;

    const ORDER_WEIGHT = 5;
    const CART_WEIGHT = 3;
    const VIEW_WEIGHT = 1;
    const SEARCH_WEIGHT = 1;

    public static function addUserLikeCategoryScore($user_id, $category_id, $score)
    {
        $user_like = self::query()->where('user_id',$user_id)->where('category_id',$category_id)->first();

        if(empty($user_like))
        {
            self::query()->insert([
                'user_id' => $user_id,
                'category_id' => $category_id,
                'score' => $score
            ]);
        }
        else
        {
            $user_like->increment('score',$score);
        }
    }

    public static function decreaseUserLikeCategoryScore($user_id, $category_id, $score)
    {
        $user_like = self::query()->where('user_id',$user_id)->where('category_id',$category_id)->first();
        if(!empty($user_like))
        {
            $user_like->decrement('score',$score);
        }
    }

}
